package com.jeecg.demo.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeecg.demo.entity.AppFinanceProductApplyEntity;
import com.jeecg.demo.entity.AppOnlineFinanceDetailEntity;
import com.jeecg.demo.entity.AppOnlineFinanceEntity;
import com.jeecg.demo.entity.AppOnlineServerDetailEntity;
import com.jeecg.demo.entity.NoncashpayJavaEntity;
import com.jeecg.demo.service.AppFinanceHomeServiceI;
import com.jeecg.demo.service.AppFinanceProductServiceI;
import com.jeecg.demo.service.AppOnlineFinanceDetailServiceI;
import com.jeecg.demo.service.AppOnlineFinanceServiceI;
import com.jeecg.demo.util.TimerUtil;

@Api(value = "AppFinanceProduct", description = "申请表", tags = "AppFinanceProduct")
@Controller
@RequestMapping("/service_financial")
public class service_financial {
	@Autowired
	private AppFinanceProductServiceI appFinanceProductServiceI;
	@Autowired
	private SystemService systemService;
	@Autowired
	private AppOnlineFinanceServiceI appOnlineFinanceServiceI;
	@Autowired
	private AppOnlineFinanceDetailServiceI appOnlineFinanceDetailServiceI;
	@Autowired
	private AppFinanceHomeServiceI appFinanceHomeServiceI;

	/**
	 * 标题图片（金融产品、业务指南）
	 * */
	@RequestMapping(value = "/getPicTitle", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "标题图片", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> PicTitle(
			@RequestParam(required = false) String type) {
		if (StringUtil.isEmpty(type)) {
			return null;
		}
		String sql = "select t.picpath,t.picurl,t.content,t.createtime,t.titlename from APP_PICTITLE t where t.titletype=? and t.isvalid=1";
		List<Map<String, Object>> listMap = systemService
				.findForJdbc(sql, type);
		return Result.success(listMap);
	}

	/**
	 * 取金融首页信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getFinanceInfo", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "金融首页信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> FinanceInfo() {
		String sql = "SELECT app_finance_home.FINANCE_ID,app_finance_home.ROLL_CONTENT,app_finance_home.PIC_PATH1,"
				+ "app_finance_home.TITLE1,app_finance_home.RESUME1,app_finance_home.PHONE1,app_finance_home.PHONE2,"
				+ "app_finance_home.PIC_PATH2,app_finance_home.TITLE2,app_finance_home.RESUME2,"
				+ "app_finance_home.PIC_PATH3,app_finance_home.TITLE3,app_finance_home.RESUME3 FROM app_finance_home";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}

	/**
	 * 金融服务列表信息
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/getFinanceServer", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "金融服务列表信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> financelist(
			@RequestParam(required = false) String type) {
		if (StringUtil.isEmpty(type)) {
			return null;
		}
		String sql;
		if (type == "0") {
			sql = "SELECT app_finance_product.FINANCE_PRODUCT_ID,app_finance_product.PRODUCT_NAME,"
					+ " app_finance_product.RESUME,app_finance_product.PIC_PATH,app_finance_product.PHONE,"
					+ " app_finance_product.PHONE1,app_finance_product.CONTENT FROM app_finance_product "
					+ " order by finance_product_id asc";
		} else {
			sql = "SELECT app_finance_product.FINANCE_PRODUCT_ID,app_finance_product.PRODUCT_NAME,"
					+ " app_finance_product.RESUME,app_finance_product.PIC_PATH,app_finance_product.PHONE,"
					+ " app_finance_product.PHONE1,app_finance_product.CONTENT FROM app_finance_product  where ISVALID=1  "
					+ " order by finance_product_id asc";
		}

		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}

	/**
	 * 保存 马上申请金融产品信息
	 * 
	 * @param userid
	 * @param username
	 * @param productid
	 * @param unit
	 * @param content
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/myapply", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "马上申请金融产品信息", produces = "application/json")
	public ResponseMessage<?> create(
			@RequestParam(required = false) int userid, // 用户编号
			@RequestParam(required = false) String username, // 姓名
			@RequestParam(required = false) String unit,// 单位
			@RequestParam(required = false) String phone, // 电话
			@RequestParam(required = false) int productid, // 融资产品
			@RequestParam(required = false) String content,// 备注
			@RequestParam(required = false) String loginname) {
		if (StringUtil.isEmpty(userid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(username)) {
			return Result.error("请输入姓名");
		}
		if (StringUtil.isEmpty(unit)) {
			return Result.error("请输入单位");
		}
		if (StringUtil.isEmpty(phone)) {
			return Result.error("请输入电话");
		}
		if (StringUtil.isEmpty(productid + "")) {
			return Result.error("请选择产品");
		}
		AppFinanceProductApplyEntity appFinanceProductApplyEntity = new AppFinanceProductApplyEntity();
		try {
			appFinanceProductApplyEntity.setAppUserId(userid);
			appFinanceProductApplyEntity.setFinanceProductId(productid);
			appFinanceProductApplyEntity.setApplyName(username);
			appFinanceProductApplyEntity.setApplyUnit(unit);
			appFinanceProductApplyEntity.setContent(content);
			appFinanceProductApplyEntity.setApplyDate(TimerUtil.getCurrentTimeStamp());
			appFinanceProductApplyEntity.setTelphone(phone);
			appFinanceProductApplyEntity.setApplyStateId("1");
			appFinanceProductServiceI.save(appFinanceProductApplyEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("申请失败");
		}
		return Result.success(appFinanceProductApplyEntity);
	}

	/**
	 * 获取 我申请的金融产品信息
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/applyList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "申请的金融产品信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> Mylist(
			@RequestParam(required = false) int userid // 用户编号
	) {
		if (StringUtil.isEmpty(userid + "")) {
			return null;
		}
		String sql = "SELECT A.APPLY_ID,A.FINANCE_PRODUCT_ID,A.APPLY_NAME,A.APPLY_UNIT,"
				+ " A.CONTENT,to_char(A.APPLY_DATE, 'yyyy-mm-dd') as APPLY_DATE,A.TELPHONE,A.APPLY_STATE_ID,"
				+ " B.PRODUCT_NAME,t.dicname FROM app_finance_product_apply A"
				+ "  inner join app_finance_product B  on (A.FINANCE_PRODUCT_ID = B.FINANCE_PRODUCT_ID) inner join SYS_DICT t"
				+ " on (t.diccode = a.apply_state_id and  t.dictype in ('FPASTATUS'))  where A.APP_USER_ID =? order by apply_id desc";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql,
				userid);
		return Result.success(listMap);
	}

	/**
	 * 获取 金融服务 咨询信息列表
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/AskList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "金融服务咨询信息列表", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> Myasklist(
			@RequestParam(required = false) int userid // 用户编号
	) {
		if (StringUtil.isEmpty(userid + "")) {
			return null;
		}
		String sql = "SELECT f.FINANCE_ONLINE_ID,to_char(f.ASK_DATE, 'yyyy-mm-dd hh24:mi:ss') as ASK_DATE ,"
                            + " f.THEME,f.APP_USER_ID,t.loginname as NICKNAME FROM app_online_finance f,V_ECP_USER t "
                            + " where f.APP_USER_ID=t.userid  and f.ISVALID=1 and f.APP_USER_ID=?"
                            + " order by f.FINANCE_ONLINE_ID desc";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql,
				userid);
		return Result.success(listMap);
	}

	/**
	 * 保存金融在线咨询主题信息
	 * 
	 * @param userid
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/addAak", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "保存金融在线咨询主题信息", produces = "application/json")
	public ResponseMessage<?> createAsk(
			@RequestParam(required = false) int userid, // 用户编号
			@RequestParam(required = false) String content// 备注
	) {
		if (StringUtil.isEmpty(userid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(content)) {
			return Result.error("问题不能为空");
		}
		AppOnlineFinanceEntity appOnlineFinanceEntity = new AppOnlineFinanceEntity();
		try {
			appOnlineFinanceEntity.setAskDate(TimerUtil.getCurrentTimeStamp());
			appOnlineFinanceEntity.setTheme(content);
			appOnlineFinanceEntity.setAppUserId(userid);
			appOnlineFinanceEntity.setIsvalid("0");
			appOnlineFinanceServiceI.save(appOnlineFinanceEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("咨询失败");
		}
		return Result.success(appOnlineFinanceEntity);
	}

	/**
	 * 保存金融在线咨询明细信息
	 * 
	 * @param questid
	 * @param userid
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/addAnswer", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "保存金融在线咨询明细信息", produces = "application/json")
	public ResponseMessage<?> addAnswer(
			@RequestParam(required = false) int userid, // 用户编号
			@RequestParam(required = false) int questid, // 问题编号
			@RequestParam(required = false) String content// 备注
	) {
		if (StringUtil.isEmpty(userid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(questid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(content)) {
			return Result.error("问题不能为空");
		}
		AppOnlineFinanceDetailEntity appOnlineFinanceDetailEntity = new AppOnlineFinanceDetailEntity();
		try {
			appOnlineFinanceDetailEntity.setFinanceOnlineId(questid);
			appOnlineFinanceDetailEntity.setSpeakId(userid);
			appOnlineFinanceDetailEntity.setOnlineResult(content);
			appOnlineFinanceDetailEntity.setSpeakDate(TimerUtil
					.getCurrentTimeStamp());
			appOnlineFinanceDetailEntity.setIsvalid("0");
			appOnlineFinanceDetailEntity.setSource(1);
			appOnlineFinanceDetailServiceI.save(appOnlineFinanceDetailEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("回复失败");
		}
		return Result.success();
	}

	/**
	 * 取金融在线咨询明细信息
	 * 
	 * @param questid
	 * @return
	 */
	@RequestMapping(value = "/AnswerList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "取金融在线咨询明细信息")
	public ResponseMessage<?> Customjson(@RequestParam(required = false) int questid // 问题编号
	) {
		System.out.println(questid);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String hql = "FROM AppOnlineFinanceDetailEntity where ISVALID=1 and FINANCE_ONLINE_ID=?"
					+ " order by SPEAK_DATE desc";
			List<AppOnlineServerDetailEntity> list = systemService.findHql(hql,
					questid);
			System.out.println(list);
			if (list.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(list);
				map.put("data", jsonArray.toArray());
			} else {
				map.put("data", "");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.success(map);
	}
	/**
     * 取结算信息
     * 
     * @return
     */
	@RequestMapping(value = "/PayServer", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "取金融结算信息")
	public ResponseMessage<?> Customjson() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String hql = "FROM AppPayBalanceEntity";
			List<AppOnlineServerDetailEntity> list = systemService.findHql(hql);
			System.out.println(list);
			if (list.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(list);
				map.put("data", jsonArray.toArray());
			} else {
				map.put("data", "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.success(map);
	}
	/**
     * 取结算信息明细
     * 
     * @return
     */
	@RequestMapping(value = "/payList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "取金融结算信息明细")
	public ResponseMessage<List<Map<String, Object>>> payList() {
			String sql = "select t.*,v.typename,d.dicname from NONCASHPAY t , v_sett_vouchertypes v, SYS_DICT d where   t.billtype=v.typeid and  d.dictype='PAYMENTSTATUS' and d.diccode=t.paymentstatus";
			List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
			return Result.success(listMap);
	}
}
